<template>
	<!-- 商品管理 -->
	<div class="page">
		<el-breadcrumb class="breadcrumb" separator-class="el-icon-arrow-right">
			<el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
			<el-breadcrumb-item>商品列表</el-breadcrumb-item>
		</el-breadcrumb>
		<el-table class="table" :data="goodsArr" border style="width: 100%">
			<el-table-column prop="goodsNo" fixed label="编号" width="170">
			</el-table-column>
			<el-table-column prop="goodsName" fixed label="商品名称" width="250" show-overflow-tooltip>
			</el-table-column>
			<el-table-column prop="goodsDesc" label="简介" show-overflow-tooltip>
			</el-table-column>
			<el-table-column prop="region" label="产地">
			</el-table-column>
			<el-table-column prop="year" label="年份">
			</el-table-column>
			<el-table-column prop="country" label="国家">
			</el-table-column>
			<el-table-column prop="vipPrice" width="90px" label="会员价(分)">
			</el-table-column>
			<el-table-column prop="winePrice" width="100px" label="酒家价格(分)">
			</el-table-column>
			<el-table-column prop="price" label="销售">
			</el-table-column>
			<el-table-column prop="rosePrice" label="销售员价格(分)" width="130">
			</el-table-column>
			<el-table-column label="状态">
				<template slot-scope="scope">
					<span v-if="scope.row.state==null">未上架</span>
					<span v-else>{{scope.row.state | filStatus}}</span>
				</template>
			</el-table-column>
			<el-table-column fixed="right" label="操作" width="190">
				<template slot-scope="scope">
					<el-button size="small" @click.native="detail(scope.row.id)" type="success">详情</el-button>
					<el-button size="small" @click.native="release(scope.row,scope.$index)" type="primary">{{scope.row.state | filStatusBtn}}</el-button>
				</template>
			</el-table-column>
		</el-table>
		<el-pagination class="page-num" background layout="prev, pager, next" :page-size="page_size" @current-change="handleCurrentChange" :total="page">
		</el-pagination>
	</div>
</template>
<script>
	import bear from "../../assets/images/bear.png";
	export default {
		name: "manage",
		data() {
			return {
				goodsArr: [],
				params: {
					"numPerPage": 10,
					"order": "desc",
					"pageNum": 1,
					"supplierStateEnum": "OPEN"
				},
				isStatus: [],
				page_size: 10, //每页显示几个
				page: 0, //总页数
				currentPage: 1, //当前页
			};
		},
		created() {

		},
		mounted() {
			this.getList();
		},
		filters:{
			filStatus:function(value){
				return value  == "TOP" ? '已发布' : '已下架';
			},
			filStatusBtn:function(value){
				return value  == "TOP" ? '下架' : '发布';
			}
		},

		methods: {
			/*分页跳转*/
			handleCurrentChange: function(currentPage) {
				this.currentPage = currentPage;
				this.getList();
			},
			// 获取列表
			getList() {
				this.params.pageNum = this.currentPage;
				this.$post("productList", this.params).then(res => {
					if(res.code == 200) {
						this.goodsArr = [];
						this.page = res.data.total;
						var list = res.data.rows;
						for(var i in list) {
							console.log(list[i])
							this.goodsArr.push(list[i]);
							this.isStatus[i] = list[i].supplierState == "OPEN" ? true : false;
							
						}
					} else {
						this.$msg.warning(res.message);
					}
				});
			},
			// 开启、关闭状态
			ctrlStatus(id) {
				this.$post("supplierQuery", {
					id: id
				}).then(res => {
					if(res.code == 200) {
						this.$msg.success(res.message);
					} else {
						this.$msg.warning(res.message);
					}
				});
			},
			// 跳转详情页面
			detail(id) {
				this.$router.push({
					name: 'updateGoods',
					params: {
						id: id
					}
				})
			},
			// 发布
			release(item, index) {
				console.log(item.state)
				var text = item.state == "TOP" ? '下架' : '发布';
				this.$confirm('是否'+text+'该商品?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					this.$post("productStatus", {
						id: item.id
					}).then(res => {
						if(res.code == 200) {
							this.goodsArr[index].state = item.state == "TOP" ? 'DOWN' : 'TOP';
							this.$msg.success(res.message);
						} else {
							this.$msg.warning(res.message);
						}
					});
				}).catch(() => {
					this.$message({
						type: 'info',
						message: '已取消发布'
					});
				});
			}
		}
	};
</script>
<style lang="less" scoped>
	.page {
		width: 100%;
		height: 100%;
		position: relative;
	}
</style>